package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.cache.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.domain.*;
import cn.wolfcode.wolf2w.interceptor.RequiredLogin;
import cn.wolfcode.wolf2w.interceptor.UserParam;
import cn.wolfcode.wolf2w.query.TravelQueryObject;
import cn.wolfcode.wolf2w.service.*;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.UMEditorUploader;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("travels")
public class TravelController {
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    @Autowired
    private ITravelCommentService travelCommentService;

    @GetMapping("query")
    public Object query(TravelQueryObject qo){
        Page<Travel> page = travelService.query(qo);
        return JsonResult.success(page);

    }

    @GetMapping("/input")
    public Object input(String id){
        // 获取所有目的地
        List<Destination> dests = destinationService.list();
        // 创建一个map集合放入游记和目的地
        Map<String , Object> map = new HashMap<>();
        if(StringUtils.hasLength(id)){
            // 如果传入过来id了,那么通过id获取当前游记
            Travel travel = travelService.get(id);
            // 把当前游记放入map中
            map.put("travel" , travel);
        }
        // 把目的地放入map中
        map.put("dests" , dests);
        return JsonResult.success(map);
    }

    //上传图片
    @RequestMapping("/contentImage")
    @ResponseBody
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }

    @RequiredLogin
    @PostMapping("saveOrUpdate")
    public Object saveOrUpdate(Travel travel , HttpServletRequest request){
        // 获取当前用户对象
        String token = request.getHeader("token");
        UserInfo userinfo = userInfoRedisService.getUserByToken(token);
        travel.setUserId(userinfo.getId());

        travelService.saveOrUpdate(travel);
        return JsonResult.success(travel.getId());
    }


    @GetMapping("/detail")
    public Object detail(String id){
        Travel travel = travelService.get(id);
        return JsonResult.success(travel);
    }


    @GetMapping("getUser")
    public Object getUser (@UserParam UserInfo userInfo){
        return JsonResult.success(userInfo);
    }

    @GetMapping("updateUser")
    public Object updateUser (UserInfo userInfo){
        return JsonResult.success(userInfo);

    }

    @PostMapping("commentAdd")
    public Object commentAdd(TravelComment comment , @UserParam UserInfo userInfo){
        BeanUtils.copyProperties(userInfo , comment);
        comment.setUserId(userInfo.getId());

        travelCommentService.save(comment);
        return JsonResult.success();

    }

    @GetMapping("comments")
    public Object comments(String travelId){
        List<TravelComment> list = travelCommentService.qeuryByTravelId(travelId);
        return JsonResult.success(list);

    }
}